<?php
class cls_data_discount
{
	var $table_name = '`discount`' ; 
	var $table_name2 = '`discountproduct`' ;
	var $table_name3 = '`product`' ;
	var $db_name = 'b2c';
	
	function countRec($fname,$where,$tablename,$table2) {
		if(!$tablename) $tablename = $this->table_name;
		if(!$table2)
		$sql = "SELECT $fname FROM $tablename $where";
		else {$sql ="select $fname from discountproduct dp join discount d on dp.discountid = d.id   $where ";
		
		}
		$result = $this->db->getAll($sql);
		
		return count($result);
	}
	
	function get_discount_list($page,$rp,$where,$sort)
	{
		if (!$page) $page = 10;
		if (!$rp) $rp = 10;
		
		$start = (($page-1) * $rp);
		
		$limit = "LIMIT $start, $rp";
		$sql = "select d.ID,d.DiscountType,d.Name,d.Description as Description,d.Amount,d.StartTime,d.EndTime,d.Status from {$this->table_name} d $where $sort $limit";
	
		$discount_list = $this->db->getAll($sql);
		

		
		return $discount_list;
	}
	
	function delete_discount($items)
	
	{
		$sql = "DELETE FROM {$this->table_name} WHERE `ID` IN ($items)";
		$total = count(explode(",",$items));
		
		$this->db->query($sql);		
	}
	
	function add_discount($items)
	{

		$item = explode(',', $items);
	 //form1.type.value+","+form1.amount.value+","+form1.name.value+","+form1.description.value+","+form1.startdate.value+","+form1.enddate.value;
	 
		$sql = "Insert into {$this->table_name} values('','{$item[0]}','{$item[2]}','{$item[3]}','{$item[1]}','','{$item[4]}','{$item[5]}',1)";
		$this->db->query($sql);
		/*get category and discount of the new discount
		$sql_getdiscount = "select * from {$this->table_name} where id in (select max(id) from {$this->table_name})";
        $result = $this->db->getAll($sql_getdiscount);
        $did = $result[0]['ID'];
		$dcategory = $result[0]['Description'];

		//get product in this category
		$sql_getprodcut = "select p.id as ProductID from product p join categorytype c on p.category = c.id where p.category = {$dcategory}";
	    $presult = $this->db->getAll($sql_getprodcut);
	    
	    //insert into discountproduct table
	    $i = 0;
	    while($i<count($presult))
	    {
	    	$sql_insert  = "Insert into discountproduct values('',{$did},{$presult[$i]['ProductID']})";
	    	
	    	$this->db->query($sql_insert);
	    	$i++;
	    }*/
	    
	    
	
	}	
	
	
	function product_add_discount($items)
	{
		
		$item = explode(',', $items);
		$discount_id = end($item);
		//p($discount_id);
		for($i=0;$i<count($item)-1;$i++)
		{
			$sql  = "insert into discountproduct values('',{$discount_id},{$item[$i]})";
			$this->db->query($sql);
		}
		
		
		//$sql  = "insert into discountproduct values('',{$item[0]})"
		
		
	}
	
	function modify_discount($items)
	{
		$sql  = "update {$this->table_name} set Status=0 where id={$items}";
		
		$this->db->query($sql);
		
		
	}
	
	
	function get_discount_product($page,$rp,$where,$sort)
	{
		if (!$page) $page = 10;
		if (!$rp) $rp = 10;
		
		$start = (($page-1) * $rp);
		
		$limit = "LIMIT $start, $rp";
		$sql = "select dp.ID,p.name as pname,d.name as dname  ,d.amount from discountproduct dp join discount d on dp.discountid = d.id join product p on dp.productid = p.id     $where $sort $limit";
		
		$discount_list = $this->db->getAll($sql);
	    
		return $discount_list;
		
		
	}
}